package com.kehutong.crm.service.impl;

import com.google.gson.reflect.TypeToken;
import com.kehutong.crm.dto.ForecastScoreRequest;
import com.kehutong.crm.dto.ForecastScoreResponse;
import com.tencentcloudapi.common.AbstractClient;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.JsonResponseModel;
import com.tencentcloudapi.common.profile.ClientProfile;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;

import java.lang.reflect.Type;

/**
 * 预测客户端
 *
 * @author wupengfei
 */
public class DemandForecastClient extends AbstractClient {

    private static final String ENDPOINT = "dataassets.tencentcloudapi.com";
    private static final String VERSION = "2021-05-19";

    private static final Logger logger = LoggerFactory.getLogger(DemandForecastClient.class);

    public DemandForecastClient(Credential credential) {
        this(credential, "", new ClientProfile());
    }

    public DemandForecastClient(Credential credential, String region) {
        this(credential, region, new ClientProfile());
    }

    public DemandForecastClient(Credential credential, String region, ClientProfile profile) {
        super(DemandForecastClient.ENDPOINT, DemandForecastClient.VERSION, credential, region, profile);
    }

    /**
     * 根据输入的md5加密后手机号返回用户的意愿等级，等级从1-5，数值最大意愿越高。
     *
     * @param mobileMd5  手机号md5
     * @param actionName 预测类型
     * @return ForecastScoreResponse
     */
    public ForecastScoreResponse request(String mobileMd5, String actionName) {
        ForecastScoreRequest request = new ForecastScoreRequest();
        request.setMobileMd5(mobileMd5);
        request.setPage(1L);
        request.setSize(1L);
        return request(request, actionName);
    }

    /**
     * 根据输入的md5加密后手机号返回用户的意愿等级，等级从1-5，数值最大意愿越高。
     *
     * @param req        ForecastScoreRequest
     * @param actionName 预测类型
     * @return ForecastScoreResponse
     */
    public ForecastScoreResponse request(ForecastScoreRequest req, String actionName) {
        JsonResponseModel<ForecastScoreResponse> responseModel = null;
        String responseStr = "";
        try {
            Type type = new TypeToken<JsonResponseModel<ForecastScoreResponse>>() {
            }.getType();
            responseStr = this.internalRequest(req, actionName);
            responseModel = gson.fromJson(responseStr, type);
        } catch (Exception e) {
            logger.error("请求腾讯预测时异常" + responseStr, e);
        }
        return responseModel != null ? responseModel.response : null;
    }

}
